package com.cloudcc.frame.comclass;


import com.cloudcc.deveditor.core.CCObject;
import com.cloudcc.deveditor.core.CCService;
import com.cloudcc.deveditor.core.UserInfo;
import com.cloudcc.deveditor.trigger.TriggerEditor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.List;

public class DistributionManager extends TriggerEditor {

    private UserInfo userInfo;
    private CCService cs;
    public DistributionManager(UserInfo userInfo){
        this.userInfo=userInfo;
        cs=new CCService(userInfo);
    }
    /*
     * @Description 分配管户经理
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param managerId 当前经理的id,ids 分配的客户的id数组;psmc 交易中心的id
     * @return: null
     **/
    public String  distribute(String param) throws Exception{
        JSONObject jsonObject = JSONObject.fromObject(param);
        String managerId = jsonObject.getString("managerId");
        String psmc = jsonObject.getString("psmc");
        String ids=jsonObject.getString("ids");
        String[] split = ids.split(",");
        String gjzt="待跟进";
        String msg="";
        String is_shared="否";
        String ghzt="未生效";
        ImportAccount importAccount = new ImportAccount(userInfo);
        if (split.length>0){
          msg="分配成功";
          for (int i = 0; i <split.length ; i++) {
              CCObject account = new CCObject("Account");
              String id=split[i];
              String code="";
              String province1="";
              String fk_city_name1="";
              String fk_country_name1="";
              String lxdz="";
              List<CCObject> account_info = cs.cquery("Account", "id='" + id + "'");
             if(account_info.size()>0){
                 code=account_info.get(0).get("invoiceTaxNo")==null?"":account_info.get(0).get("invoiceTaxNo")+"";
                 province1=account_info.get(0).get("province")==null?"":account_info.get(0).get("province")+"";
                 fk_city_name1=account_info.get(0).get("fk_city_name")==null?"":account_info.get(0).get("fk_city_name")+"";
                 fk_country_name1=account_info.get(0).get("fk_country_name")==null?"":account_info.get(0).get("fk_country_name")+"";
                 lxdz=account_info.get(0).get("registeredAddress")==null?"":account_info.get(0).get("registeredAddress")+"";
             }
              account.put("id",id);
              account.put("ownerid",managerId);
              account.put("follow_status",gjzt);
              cs.update(account);
              CCObject manage_group = new CCObject("manage_group");
              manage_group.put("name", code);
              manage_group.put("fk_ccuser", managerId);
              manage_group.put("fk_account", id);
              manage_group.put("fk_psmc", psmc);
              manage_group.put("province1", province1);
              manage_group.put("fk_city_name1", fk_city_name1);
              manage_group.put("fk_country_name1", fk_country_name1);
              manage_group.put("lxdz", lxdz);
              manage_group.put("is_shared", is_shared);
              manage_group.put("ghzt", ghzt);
              String gen = "";
              try {
                  gen = importAccount.gen(lxdz);
              } catch (Exception e) {
                  e.printStackTrace();
              }
              String[] split2 = gen.split(",");
              manage_group.put("dlwzco", split2[0]);
              manage_group.put("dlwzla", split2[1]);
              cs.insert(manage_group);
          }
      }
      return msg;
    }
    /*
     * @Description:获取可以分配的员工
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param null:
     * @return: null
     **/
    public String getManagers() throws Exception{
        //获取当前操作用户的userid
        String userId = userInfo.getUserId();
        //查询 机构负责人是当前操作用户的 项目经理
        String sql="select * from ccuser where is_deleted='0'" +
                " and jgfzr='"+userId+"'";
        List<CCObject> ccuser = cs.cqlQuery("ccuser", sql);
        JSONArray rs = new JSONArray();
        if(ccuser.size()>0){
            for (int i=0;i<ccuser.size();i++){
                JSONObject jsonObject = new JSONObject();
                String value = ccuser.get(i).get("id") == null ? "" : ccuser.get(i).get("id") + "";
                String label = ccuser.get(i).get("name") == null ? "" : ccuser.get(i).get("name") + "";
                jsonObject.put("value",value);
                jsonObject.put("label",label);
                rs.add(jsonObject);
            }
        }
        return rs.toString();
    }

/*
 * @Description:获取公共交易中心的资源，通过选中的id，返回交易中心的name和id
 * @T(n):
 * @Author: zxb
 * @Date:
 * @param null:
 * @return: null
 **/
    public String getPsmc(String userid) throws Exception{
        //查询 机构负责人是当前操作用户的 项目经理
        List<CCObject> ghjl = cs.cquery("ghjl", "yh='"+userid+"'");
        JSONArray rs = new JSONArray();
        if(ghjl.size()>0){
            for (int i=0;i<ghjl.size();i++){
                JSONObject jsonObject = new JSONObject();
                String value = ghjl.get(i).get("jyzx") == null ? "" : ghjl.get(i).get("jyzx") + "";
                String label = ghjl.get(i).get("jyzxccname") == null ? "" : ghjl.get(i).get("jyzxccname") + "";
                jsonObject.put("value",value);
                jsonObject.put("label",label);
                rs.add(jsonObject);
            }
        }
        return rs.toString();
    }
}
